package com.project2.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.project2.bean.CommonData;
import com.project2.bean.PageBo;
import com.project2.bean.po.MarketStorage;
import com.project2.bean.po.MarketStorageExample;
import com.project2.mapper.MarketStorageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class AdminStorageServiceImpl implements AdminStorageService {

    @Autowired
    MarketStorageMapper marketStorageMapper;

    @Override
    public MarketStorage addpic(MultipartFile myFile) {

        long size = myFile.getSize();//图片大小
        String originalFilename = myFile.getOriginalFilename();//图片name
        String[] split = originalFilename.split("\\.");
        String random = UUID.randomUUID().toString().replaceAll("-", "");
        String key = random + "." + split[split.length - 1];//key
        Date date = new Date();//当前时间
        String contentType = myFile.getContentType();//type
        String url =  "http://localhost:8083/"+key;//ur;

        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setKey(key);
        marketStorage.setName(originalFilename);
        marketStorage.setType(contentType);
        marketStorage.setSize((int) size);
        marketStorage.setAddTime(date);
        marketStorage.setUpdateTime(date);
        marketStorage.setId(null);
        marketStorage.setDeleted(false);
        marketStorage.setUrl(url);

        marketStorageMapper.insertSelective(marketStorage);

        //获取jar包所在目录
//        ApplicationHome h = new ApplicationHome(getClass());
//        File jarF = h.getSource();
//
//        //在jar包所在包下生成一个upload文件夹用来存储上传的图片
//        String parent = jarF.getParentFile().toString() + "\\classes\\static\\image\\book\\";
//
//
//        File file = new File(parent);
//
//        try {
//            myFile.transferTo(file);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


        //将头像保存在文件夹
//        File dir = new File(parent);
//        if (!dir.exists()){
//            dir.mkdirs();
//        }


        File file = new File("D://img/", key);
        try {
            myFile.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return marketStorage;
    }

    /**
     *@Description:这个是显示存储数据表的所有信息和根据name模糊查询,
     *              根据key精准查询的具体实现
     *@Param:
     *@return:
     *@date: 2022/8/13
     *@Author: MH
     */
    @Override
    public CommonData<MarketStorage> showAllStorageInfo(String name,String key,PageBo pageBo) {
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        if (name != null) {
            criteria.andNameLike("%"+name+"%");
        }
        if (key != null) {
            criteria.andKeyEqualTo(key);
        }
        criteria.andDeletedEqualTo(false);
        marketStorageExample.setOrderByClause(pageBo.getSort() + " " + pageBo.getOrder());
        PageHelper.startPage(pageBo.getPage(), pageBo.getLimit());
        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(marketStorageExample);
        PageInfo marketStoragePageInfo = new PageInfo(marketStorages);
        return CommonData.data(marketStoragePageInfo);
    }

    /**
     *@Description:这个是修改对象名称属性的具体实现,根据前端传过来的完整的MarketStorage对象,
     *              直接根据id进行更新全部数据,然后再回显更新后的数据
     *@Param:
     *@return:
     *@date: 2022/8/13
     *@Author: MH
     */
    @Override
    public List<MarketStorage> updateStorageName(MarketStorage marketStorage) {
        MarketStorageExample marketStorageExample = new MarketStorageExample();
        marketStorageMapper.updateByPrimaryKey(marketStorage);
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();
        criteria.andIdEqualTo(marketStorage.getId());
        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(marketStorageExample);
        return marketStorages;
    }

    /**
     *@Description:这个是对对象进行逻辑删除的具体实现,直接将Deleted改为true即可
     *@Param:
     *@return:
     *@date: 2022/8/13
     *@Author: MH
     */
    @Override
    public void deleteStorage(MarketStorage marketStorage) {
        marketStorage.setDeleted(true);
        marketStorageMapper.updateByPrimaryKey(marketStorage);
    }

//    public
}
